import { PrismaClient } from "@prisma/client";
import bc from "bcryptjs"
import { faker } from "@faker-js/faker"
import { UniqueEnforcer } from "enforce-unique";
import { join } from "@prisma/client/runtime/library";

const p = new PrismaClient()

await p.user.createMany({
    data: [
        {name: "Bill Gates", email: "bill@email.com", password:bc.hashSync("password",8)},
        {name: "Jackie White", email: "jackie@email.com", password:bc.hashSync("password",8)}
    ]
})

await p.department.createMany({
    data: [
        {id: 1, name:"IT", email:"it@myschool.com",telephone:"0871-88909091"},
        {id: 2, name:"Construction", email:"construction@myschool.com",telephone:"0871-88909092"},
        {id: 3, name:"Chemistry", email:"chemistry@myschool.com",telephone:"0871-88909093"},
    ]
})

await p.club.createMany({
    data: [
        {name: "Webpage Design",capacity: 30, dept_id: 1},
        {name: "Database Basic",capacity: 40, dept_id: 1},
        {name: "Networking",capacity: 50, dept_id: 1},
        {name: "Room Decoration",capacity: 30, dept_id: 2},
        {name: "Contruction Materials",capacity: 30, dept_id: 2},
        {name: "Chemistry Industy",capacity: 30, dept_id: 3},
        {name: "Experiment Tech",capacity: 40, dept_id: 3},
    ]
})

//let members = []


for(let i=1; i<=100; i++){
    const clubs = await p.club.findMany({
        select: {
            id: true,
            capacity: true,
            members: true
        }
    })
    
    
    const availableClubs = clubs.filter(c=>c.members.length < c.capacity)
    //console.log(availableClubs);
    

    const clubIds = faker.helpers.arrayElements(availableClubs,faker.helpers.arrayElement([1,2,3]))
    let clubIdDates = clubIds.map(c=>({id:c.id,joinDate: faker.date.between({from: '2020-01-01',to:'2024-10-01'})}))
    let _clubs = []
   
    
    for(let club of clubIdDates){
        _clubs.push({
            joinDate: club.joinDate,
            club: {
                connect: {id: club.id}
            }
        })
    }
    //console.log(_clubs);
    //console.log(clubs);
    
    const enforcer = new UniqueEnforcer()
    const mobile = enforcer.enforce(()=>{
        return faker.helpers.fromRegExp('1[39|33|87|89][0-9]{9}')
    }) 
    const gender = faker.helpers.arrayElement(["Male","Female"])
    const member = {
        firstname: faker.person.firstName(gender.toLowerCase()),
        lastname: faker.person.lastName(),
        gender,
        mobile,
        clubs: {
            create: _clubs
        } 
    }
    await p.member.create({
        data: member
    })

    //members.push(member)
}
//console.table(members)


// for(let member of members){
//     await p.member.create({
//         data: member
//     })
// }